#include "sigwid.h"
#include "ui_sigwid.h"
#include <QSize>
#include <QDebug>
#include<iostream>
#include<fstream>
#include<QDebug>
#include<QString>
#include<QMessageBox>
#include <QKeyEvent>
#include<QFile>
#include<QCoreApplication>
#include<QStackedWidget>
#include<QButtonGroup>
#include"poco/user.h"
#include"poco/organizationInfo.h"
#include"controller/managercontroller.h"
#include"controller/merchantcontroller.h"

sigwid::sigwid(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::sigwid)
{

    setWindowIcon(QIcon(":/new/prefix1/logo_win.png"));
    ui->setupUi(this);
    this->setWindowTitle("注册界面");
    this->resize(QSize(700,600));
    //设置窗口渐变色
    //this->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(251,102,102, 200), stop:1 rgba(20,196,188, 210));");
    //设置背景图片
    this->setStyleSheet("#sigwid{border-image:url(:/new/prefix1/shitang.jpg);}");
    //this->setStyleSheet("#sigwid{border-image:(:/new/prefix1/shitang.jpg);}");

    QButtonGroup *group = new QButtonGroup(this);
    group->addButton(ui->sig_checkBox_2,1);
    group->addButton(ui->sig_checkBox_3,2);
    ui->stackedWidget->setCurrentIndex(1);
}

sigwid::~sigwid()
{
    delete ui;
}


void sigwid::on_sig_ok_clicked()
{

    //获得用户类型
    User user;//用户
    OrganizationInfo org;//组织信息
    QString sign=nullptr;
//    if(ui->sig_checkBox->isChecked()){
//        sign=QString("user");
//        qDebug()<< QString("用户类型为就餐者");
//    }
    //获得用户id
    QString id=ui->sig_usrname->text();

    user.phone=id.toStdString();
    if(id.length()==0)
    {
        QMessageBox::warning(this,tr("failed"),tr("用户名不能为空！"),QMessageBox::Yes);
        return;
    }
    else
    {
        qDebug()<< QString("用户名称为:")<<id;
    }
    //获得用户密码
    QString pwd=ui->sig_pwd->text();
    QString pwd2=ui->sig_cppwd->text();
    if(pwd.length()!=pwd2.length()){
        QMessageBox::warning(this,tr("failed"),tr("两次密码输入不一致！"),QMessageBox::Yes);
        return;
    }
    else
    {
        qDebug()<< QString("用户密码为:")<<pwd;
    }
    user.password=pwd.toStdString();

    //获得用户性别
    //QString sex=ui->
    QString sex=ui->sig_sex->currentText();
    user.sex=sex.toStdString();

    //获得用户姓名
    QString name=ui->sig_mingzi->text();
    user.name=name.toStdString();

    //获得银行卡号
    QString creCard=ui->sig_credcard->text();
    user.creditCard=creCard.toStdString();

    //获得身份证号
    QString IDcard=ui->sig_idCard->text();
    user.identityCard=IDcard.toStdString();

    //组织
    //组织id
//    QString oID=ui->sig_oid->text();
//    org.id=oID.toInt();

    //组织名
    QString oname=ui->sig_oname->text();
    org.name=oname.toStdString();

    //组织地址
    QString oadd=ui->sig_add->text();
    org.address=oadd.toStdString();

    //组织卡号
    QString ocrecard=ui->sig_ocrecard->text();
    org.creditCard=ocrecard.toStdString();


    int flag=1;
    if(ui->sig_checkBox_2->isChecked()){
        sign=QString("business");
        qDebug()<< QString("用户类型为商家");
        MerchantController merCon;
        user.type=User::TYPE_merchant;
        flag=merCon.registerUser(user);
    }
    else if(ui->sig_checkBox_3->isChecked()){
        sign=QString("manager");
        qDebug()<< QString("用户类型为管理员");
        ManagerController manCon;
        user.type=User::TYPE_administrator;
        manCon.registerUser(user,org);
    }
    else{
        qDebug()<< QString("用户未选择类型");
        QMessageBox::warning(this,tr("failed"),tr("未选择类型，请重新选择"),QMessageBox::Yes);
        return;
    }

    //_todo_数据库,注册函数
    if(flag)
    {
        QMessageBox::information(this,tr("success"),tr("您已注册，请返回登陆"),QMessageBox::Yes);
        this->close(); //关闭注册界面
    }



}

void sigwid::on_sig_exit_clicked()
{
    this->close();
}


void sigwid::on_sig_checkBox_3_stateChanged(int arg1)
{
    ui->stackedWidget->setCurrentIndex(0);
}

void sigwid::on_sig_checkBox_2_stateChanged(int arg1)
{
    ui->stackedWidget->setCurrentIndex(1);
}
